/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.comm.message.impl;

import org.kopsox.comm.message.Message;
import org.kopsox.comm.message.TextMessage;
import org.kopsox.general.priority.Priority;
import org.kopsox.general.priority.PriorityEnum;
import org.kopsox.general.type.MessageType;
import org.kopsox.general.type.MessageTypeEnum;

/**
 * @author Konrad Renner - 22.06.2010
 *
 */
public class TextMessageImpl implements TextMessage {
	
	private static final long serialVersionUID = -42073141862330169L;
	private final String message;
	private Priority priority;
	private transient Object messageSender;
	private String namespace;
	
	public TextMessageImpl(String message){
		this(message,PriorityEnum.MEDIUM.getPriority());
	}
	
	public TextMessageImpl(String message,Priority priority){
		this.message = message;
		this.priority = priority;
		this.namespace = null;
		this.messageSender = null;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.message.TextMessage#getMessage()
	 */
	@Override
	public String getMessage() {
		return this.message;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.message.Message#getMessageSender()
	 */
	@Override
	public Object getMessageSender() {
		return this.messageSender;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.message.Message#getNameSpace()
	 */
	@Override
	public String getNameSpace() {
		return this.namespace;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.message.Message#setMessageSender(java.lang.Object)
	 */
	@Override
	public void setMessageSender(final Object sender) {
		this.messageSender = sender;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.general.type.Typed#getType()
	 */
	@Override
	public MessageType getType() {
		return MessageTypeEnum.TEXT;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.general.priority.Prioritized#getPriority()
	 */
	@Override
	public Priority getPriority() {
		return this.priority;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.general.priority.Prioritized#setPriority(org.kopsox.general.priority.Priority)
	 */
	@Override
	public void setPriority(final Priority priority) {
		this.priority = priority;
	}

	/**
	 * Sets the namespace
	 * @param namespace
	 */
	public void setNameSpace(final String namespace) {
		this.namespace = namespace;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.message == null) ? 0 : this.message.hashCode());
		result = prime * result
				+ ((this.namespace == null) ? 0 : this.namespace.hashCode());
		result = prime * result
				+ ((this.priority == null) ? 0 : this.priority.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TextMessageImpl other = (TextMessageImpl) obj;
		if (this.message == null) {
			if (other.message != null)
				return false;
		} else if (!this.message.equals(other.message))
			return false;
		if (this.namespace == null) {
			if (other.namespace != null)
				return false;
		} else if (!this.namespace.equals(other.namespace))
			return false;
		if (this.priority == null) {
			if (other.priority != null)
				return false;
		} else if (!this.priority.equals(other.priority))
			return false;
		return true;
	}


	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("TextMessageImpl:");
		sb.append("message=");
		sb.append(this.message);
		sb.append(",namespace=");
		sb.append(this.namespace);
		sb.append(",priority=");
		sb.append(this.priority.toString());
		sb.append(",messageSender=");
		sb.append(this.messageSender);
		sb.append(";");
		return sb.toString();
	}
	
	@Override
	public int compareTo(Message o) {
		int erg = this.priority.compareTo(o.getPriority());
		if(erg == 0) {
			erg = this.getType().toString().compareTo(o.getType().toString());
			
			if(erg == 0) {
				erg = this.getClass().getName().compareTo(o.getClass().getName());
			}
		}
		return erg;
	}
}
